<?php
namespace Common\Controller;
use Think\Controller;

class BaseController extends Controller{

    public function _initialize(){
        if (C('DEFAULT_THEME')) {
            $THEME_PATH = '/tpl/'.MODULE_NAME.'/'.C('DEFAULT_THEME').'/';
        }else{
            $THEME_PATH = '/tpl/'.MODULE_NAME.'/';
        }
        
        define('RES', $THEME_PATH . 'common');
        define('STATICS', '/tpl/' . 'static');

        //数据缓存
        if (S('C_DATA')) {
            $this->assign('c_data',S('C_DATA'));
        }else{
            $arr = array();
            $data = M('OptionCate')->select();
            foreach ($data as $key => $value) {
                $arr[$value['value']] = M("OptionItem")->field('id,name,value')->where(array('cate'=>$value['id']))->order('sort asc,id asc')->select();
            }
            S('C_DATA',$arr);
        }   
        $this->assign('c_data',S('C_DATA'),2592000);
    }

    //返回json数据
    public function echo_json_str($state , $message , $url=''){
		if(empty($url)){
			$url = '';	
		}
		$josn_arr = array(
                        'state' =>   $state,
                        'message' => $message,
                        'url' => $url,
                        ); 
        return json_encode($josn_arr);
        die;
	}

	//验证码显示
	public function verify() {
        ob_clean();
        $Verify = new \Think\Verify();     
        $Verify->length   = 4; 
        $Verify->fontSize = 100;
        $Verify->useCurve = false;//关闭干扰线
        $Verify->entry();
	}

    //验证验证码
    function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

	//公用插入数据
	protected function all_add($name = '', $back = '/index'){
        $name = $name ? $name : MODULE_NAME;
        $obj = D($name);
        if ($data = $obj->create()) {
            if ($list = $obj->add($data)) {
                $this->success('操作成功',$back);
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error($obj->getError());
        }
    }

    //公用更新数据
    protected function all_save($name = '', $back = '/index'){        
        $name = $name ? $name : MODULE_NAME;
        $obj = D($name);
        if ($data = $obj->create()) {
            if ($list = $obj->save($data)) {                
                $this->success('操作成功',$back);
            } else {
                $this->error('操作失败');
            }            
        } else {
            $this->error($obj->getError());
        }
    }  

    //公用删除数据
    protected function all_del($name = '', $back = '/index'){

        $id=I('post.selectedids');
        if($id==''){
            $this->error('您没有选择任何信息！');
        }else{
            $map['id'] = array('in',$id);
            $obj = M($name);
            $list = $obj->where($map)->delete();
            if ($list) {
                $this->success('操作成功',$back);
            }else{
                $this->error('操作失败');
            }
        }
    }   

    protected function getUserMoney($userid){
        $finance = M('Finance');
        $map['memberID'] = $userid;
        $map['type'] = 1;
        $chongzhi = $finance->where($map)->sum('money'); //充值金额
        if (!$chongzhi) {
            $chongzhi = 0;
        }

        $map['memberID'] = $userid;
        $map['type'] = 3;
        $koukuan = $finance->where($map)->sum('money'); //会员扣款
        if (!$koukuan) {
            $koukuan = 0;
        }

        $map['type'] = 2;
        $buy = $finance->where($map)->sum('money'); //购买记录
        if (!$buy) {
            $buy = 0;
        }

        return array(
                'chongzhi' =>$chongzhi,
                'buy' =>$buy,
                'koukuan' =>$koukuan,
                'money' => $chongzhi - $buy - $koukuan
            );

    }
}